﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
using System.Xml.Serialization;

using Telerik.Sitefinity.Web.Configuration;
using Telerik.Sitefinity.Configuration;
using System.Configuration;
using Telerik.Sitefinity.Configuration.Data;

namespace Migration.Classes
{
	public static class MigrationThemes
	{
		private static ConfigManager m_ConfigManager;
		private static string[] m_ReplacedCssSelector = { "RadPanelBar", "RadTabString", "RadGrid", "GridHeader", "GridRow", "GridAltRow", "SelectedRow", "ActiveRow" };

		static MigrationThemes()
		{
			m_ConfigManager = ConfigManager.GetManager();
		}

		public static void ImportThemes(List<Telerik.Sitefinity.Services.Migration.MigrationTheme> themeContents)
		{
			if (m_ConfigManager.Provider == null)
			{
				m_ConfigManager = ConfigManager.GetManager();
			}

			var config = m_ConfigManager.GetSection<AppearanceConfig>();

			if (!config.FrontendThemes.ContainsKey("Basic"))
			{
				ThemeElement basicTheme = new ThemeElement(config.FrontendThemes);
				basicTheme.Name = "Basic";
				basicTheme.Namespace = "Telerik.Sitefinity.Resources.Themes.Basic";
				basicTheme.AssemblyInfo = "Telerik.Sitefinity.Resources.Reference, Telerik.Sitefinity.Resources";
				config.FrontendThemes.Add(basicTheme);
			}

			if (!config.BackendThemes.ContainsKey("Default"))
			{
				ThemeElement backendTheme = new ThemeElement(config.BackendThemes);
				backendTheme.Name = "Default";
				backendTheme.Namespace = "Telerik.Sitefinity.Resources.Themes.Default";
				backendTheme.AssemblyInfo = "Telerik.Sitefinity.Resources.Reference, Telerik.Sitefinity.Resources";
				config.BackendThemes.Add(backendTheme);
			}

			string rootFolder = HttpContext.Current.Server.MapPath("~/App_Data/Sitefinity/WebsiteTemplates") + "\\";

			for (int i = 0; i < themeContents.Count; i++)
			{
				string themeName = themeContents[i].ThemeName;
				
				if (config.FrontendThemes.ContainsKey(themeName))
				{
					MigrationLogger.AppendLogMessage(String.Format("Skipping \"<b>{0}</b>\", already exists.<br/>", themeName));
					continue;
				}

				MigrationLogger.AppendLogMessage(String.Format("Importing \"<b>{0}</b>\".<br/>", themeName));

				ThemeElement themeElement = new ThemeElement(config.FrontendThemes);
				themeElement.Name = themeName;
				themeElement.Path = String.Format("~/App_Data/Sitefinity/WebsiteTemplates/{0}/App_Themes/{0}/", themeName);
				config.FrontendThemes.Add(themeElement);

				Directory.CreateDirectory(rootFolder + themeName);
				Directory.CreateDirectory(rootFolder + themeName + "\\App_Master");
				Directory.CreateDirectory(rootFolder + themeName + "\\App_Themes");
				Directory.CreateDirectory(rootFolder + themeName + "\\App_Themes\\" + themeName);
				Directory.CreateDirectory(rootFolder + themeName + "\\App_Themes\\" + themeName + "\\Global");
				Directory.CreateDirectory(rootFolder + themeName + "\\App_Themes\\" + themeName + "\\Images");
				Directory.CreateDirectory(rootFolder + themeName + "\\App_Themes\\" + themeName + "\\Styles");
				Directory.CreateDirectory(rootFolder + themeName + "\\ControlTemplates");

				for (int j = 0; j < themeContents[i].CssFiles.Count; j++)
				{
					string cssFileName = themeContents[i].CssFiles[j].FileName;
					string cssFileContents = themeContents[i].CssFiles[j].FileContents;

					// Replace class selectors ending with _Default with _<themename> => .RadPanelBar_Default -> .RadPanelbar_tiu
					foreach (var selector in m_ReplacedCssSelector)
					{
						cssFileContents = cssFileContents.Replace("." + selector + "_Default", "." + selector + "_" + themeName.ToLower());
					}

					File.WriteAllText(rootFolder + themeName + "\\App_Themes\\" + themeName + "\\Global\\" + cssFileName, cssFileContents);
				}
			}
			
			m_ConfigManager.SaveSection(config);
		}
	}
}
